<template>
  <div class="container">
    <div class="app-container">
      <header>
        <el-button size="small" type="primary" @click="isAddVisible = true">添加角色</el-button>
      </header>
      <!-- section和div很像，唯一的区别：section所代表的区域比div大 -->
      <section>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column align="center" label="角色" width="200">
            <template slot-scope="scope">
              <span v-if="!scope.row.isEdit">{{ scope.row.name }}</span>
              <input v-else v-model="scope.row.editRow.name" style="width:150px" type="text" />
            </template>
          </el-table-column>
          <!-- <el-table-column align="center" prop="state" label="启用" width="300" /> -->
          <!-- 第一种：插槽 -->
          <!-- <el-table-column align="center" prop="state" label="启用" width="300">
            <template slot-scope="scope">
              <span>{{ scope.row.state === 1 ? '已启用' : '未启用' }}</span>
            </template>
          </el-table-column>-->
          <!-- 第二种：element-ui一个方法属性 -->
          <!-- <el-table-column
            align="center"
            :formatter="formatState"
            prop="state"
            label="启用"
            width="300"
          />-->
          <!-- 第三种：过滤器 -->
          <el-table-column align="center" prop="state" label="启用" width="200">
            <template slot-scope="scope">
              <span v-if="!scope.row.isEdit">{{ scope.row.state | handleState }}</span>
              <el-switch
                v-else
                v-model="scope.row.editRow.state"
                active-color="#409eff"
                inactive-color="#dcdfe6"
                :active-value="1"
                :inactive-value="0"
              />
            </template>
          </el-table-column>

          <el-table-column align="center" prop="description" label="描述" width="400">
            <template slot-scope="scope">
              <span v-if="!scope.row.isEdit">{{ scope.row.description }}</span>
              <el-input
                v-else
                v-model="scope.row.editRow.description"
                type="textarea"
                autosize
                placeholder="请输入内容"
              />
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <div v-if="!scope.row.isEdit">
                <el-button
                  type="text"
                  size="small"
                  @click="$refs.DistributionAuthTree.open(scope.row.id)"
                >分配权限</el-button>
                <el-button type="text" size="small" @click="editFn(scope.row)">编辑</el-button>
                <el-popconfirm
                  title="这是一段内容确定删除吗？"
                  style="margin-left:10px"
                  @onConfirm="delFn(scope.row.id)"
                >
                  <el-button slot="reference" type="text" size="small">删除</el-button>
                </el-popconfirm>
              </div>
              <div v-else>
                <el-button size="mini" type="primary" @click="edit(scope.row)">确认</el-button>
                <el-button size="mini" @click="scope.row.isEdit = false">取消</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination
            layout="prev, pager, next"
            :page-size="pageSize"
            :total="total"
            @current-change="handleCurrentChange"
          />
        </div>
      </section>
      <!-- 分配权限框 -->
      <distribution-auth-tree ref="DistributionAuthTree" />
      <!-- 添加角色框 -->
      <add-role :is-add-visible.sync="isAddVisible" @success="success" />
    </div>
  </div>
</template>
<script>
import { getRoleList, update, delRole } from '@/api/role'
// import DistributionAuth from './components/distributionAuth.vue'
import DistributionAuthTree from './components/distributionAuthTree.vue'
import AddRole from './components/addRole.vue'
export default {
  name: 'Role',
  filters: {
    handleState: function(value) {
      // console.log(value)
      return value === 1 ? '已启用' : '未启用'
      // return '已启用'
    }
  },
  components: {
    // DistributionAuth,
    AddRole,
    DistributionAuthTree
  },
  data() {
    return {
      roleId: 0,
      tableData: [],
      pageNum: 1,
      pageSize: 6,
      total: 0,
      isVisible: false,
      isAddVisible: false

    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { data } = await getRoleList({
        page: this.pageNum,
        pagesize: this.pageSize
      })
      // 判断当前也是不是有数据
      if (data.rows.length === 0 && this.pageNum > 1) {
        this.pageNum--
        this.getRoleList()
        return
      }
      // console.log(data)
      this.total = data.total
      // console.log(data.total)
      this.tableData = data.rows
      // 处理服务器返回的数据
      this.tableData.forEach(item => {
        this.$set(item, 'isEdit', false)
      })
    },
    // element-ui中table中的方法属性格式化数据
    formatState(row, column, cellValue, index) {
      return cellValue === 1 ? '已启用' : '未启用'
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.getRoleList()
    },
    // 点击编辑里面的确认按钮发送请求
    async edit(row) {
      // console.log(row)
      // row.isEdit = !row.isEdit
      const obj = { ...row.editRow }

      await update(obj)
      // eslint-disable-next-line require-atomic-updates
      // row.name = row.editRow.name
      // eslint-disable-next-line require-atomic-updates
      // row.state = row.editRow.state
      // eslint-disable-next-line require-atomic-updates
      // row.description = row.editRow.description
      Object.assign(row, row.editRow)
      console.log(1)
      // eslint-disable-next-line require-atomic-updates
      row.isEdit = false
    },
    async delFn(id) {
      console.log(id)
      // 删除
      await delRole(id)
      // 提示
      this.$message.success('删除成功！')
      this.getRoleList()
    },
    success() {
      // 刷新
      this.getRoleList()
    },
    // 点编辑
    editFn(row) {
      // 深拷贝row的数据
      const o = Object.assign({}, row)
      delete o.isEdit
      this.$set(row, 'editRow', o)
      row.isEdit = true
    }

  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding-bottom: 20px;
}
header {
  padding: 10px;
}
.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 15px;
}
</style>
